﻿using System;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.Windows.Forms;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmPrintCard : Form
    {
        int _EmployeeID;
        //int b = 0;
        private Bitmap bmpImage;
                
        public frmPrintCard(int EmployeeID)
        {
            InitializeComponent();
            _EmployeeID = EmployeeID;

            MainServiceClient _wcfMain = Service.MainService();
            DataTable dt = _wcfMain.Query("Select Employee_FullName,Department_Name, Position_Name from vw_HRM_EmployeeActive where Employee_ID = " + _EmployeeID.ToString()).Tables[0];
            string _Name = UISetting.GetEnglishString(dt.Rows[0]["Employee_FullName"].ToString()).ToUpper();
            string _Dept = dt.Rows[0]["Department_Name"].ToString();
            string _Pos = dt.Rows[0]["Position_Name"].ToString();
            _wcfMain.Close();
            drawPrintPage(_Name, _Dept, _Pos, "www.hanoi-daewoohotel.com");
           
            printDocument1.PrintPage +=new PrintPageEventHandler(printDocument1_PrintPage);
            
            this.printPreviewControl1.Document = printDocument1;
            LoadLanguage();
        }

        private Image GetEmployeeImage(int _employeeID)
        {
            Image _img = null;
            EmployeeClient wcfEmployee = Service.Employee();
            byte[] btarr = wcfEmployee.Employee_GetImage(_employeeID);
            if (btarr != null)
            {
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream(btarr, 0, btarr.Length))
                {
                    ms.Write(btarr, 0, btarr.Length);
                    //Set image variable value using memory stream.
                    _img = Image.FromStream(ms, true);
                }
            }
            wcfEmployee.Close();
            return _img;
        }
        
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            try
            {
                float x = 0;
                float y = 0;
                e.Graphics.DrawImage((Image)bmpImage, x, y);
                e.HasMorePages = false;
            }
            catch
            {
                if (MessageBox.Show("Không tìm thấy dữ liệu cấu hình in", "Lỗi", MessageBoxButtons.OK) == DialogResult.OK)
                    this.Close();
            }
        }

        private void drawPrintPage(string _Name, string _Dept, string _Position, string _Website)
        {
            //-----Tạo 1 ảnh gốc---------------------------------------------------------------------------|
            //Vary the resolution until you get the quality you desire. 
            bmpImage = new Bitmap(540, 860);
            //Độ phân giải Resolution
            bmpImage.SetResolution(240, 240);

            Graphics g = Graphics.FromImage(bmpImage);
            g.Clear(Color.White);
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
            //---------------------------------------------------------------------------------------------|

            //Định dạng font chữ

            Font F10 = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            Font F8 = new System.Drawing.Font("Microsoft Sans Serif", 8F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            Font F9 = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

            //Đặt màu cho chữ
            SolidBrush drawBrush = new SolidBrush(Color.Black);
            SolidBrush drawBrushForBorder = new SolidBrush(System.Drawing.Color.FromArgb(((int)(((byte)(121)))), ((int)(((byte)(121)))), ((int)(((byte)(123))))));
            //Đặt tọa độ cho chữ
            Point _NamePoint = new System.Drawing.Point(18, 585);
            Point _PosPoint = new System.Drawing.Point(18, 645);
            Point _DeptPoint = new System.Drawing.Point(18, 705);
            Point _WebPoint = new System.Drawing.Point(18, 745);
            try
            {
                Image imgLogo = HRM.Properties.Resources.logo;
                //Image imgLogo = HRM.Properties.Resources.Logo_Daewoo___Kumho_HiRes;
                Image imgofEmployee = GetEmployeeImage(_EmployeeID);
                //Vẽ ảnh
                g.DrawImage(imgLogo, -15, 40, 540, 142);//ảnh logo
               // g.DrawImage(imgLogo, 0, 0, imgLogo.Width/7, imgLogo.Height/7);//ảnh logo
                g.DrawImage(imgofEmployee, 140, 190, 260, 342);//ảnh nhân viên
            }
            catch
            {
                MessageBox.Show("Không tìm thấy dữ liệu ảnh nhân viên", "Lỗi");
            }
            /*
            |--------410------------|
            |-----------------------|--|--|
            |          LOGO            |  |
            |                          | 190
            |---130-|------260---- -|  |  |
            |=======|===============|  |--|
            |       |               |  |  |
            |       |               |  |  |
            |       | Ảnh Nhân viên |  | 342
            |       |               |  |  |
            |       |               |  |  |
            |       |               |  |  |
            |=======|===============|  |--|
            |                          |
            | HỌ VÀ TÊN                |
            |                          |
            | Phòng ban                |
            | Chức vụ                  |
            |                          |
            | Website                  |
            |                          |
            |--------------------------|               
            */

            //Vẽ các đường thẳng
            g.DrawLine(new Pen(drawBrushForBorder, 10), 0, 180, 415, 180); //Đường thẳng trên
            g.DrawLine(new Pen(drawBrushForBorder, 10), 0, 200 + 342, 415, 200 + 342); //Đường thẳng dưới
            g.DrawLine(new Pen(drawBrushForBorder, 10), 410, 180, 410, 200 + 342); //Đường thẳng đứng
            // Vẽ đường viền
            //g.DrawRectangle(new Pen(drawBrush, 2), 0, 0, 510, 820);

            // Vẽ tên, phòng ban, chức vụ
            g.DrawString(_Name.ToUpper(), F10, drawBrush, _NamePoint);//họ tên nhân viên
            g.DrawString(_Position, F8, drawBrush, _PosPoint);//chức vụ
            g.DrawString(_Dept, F9, drawBrush, _DeptPoint); //phòng ban
            
            //g.DrawString(_Website, F8, drawBrush, _WebPoint);//Website

            //|----------Tạo 1 ảnh (g1) đè lên ảnh gốc (g) -----------------------------------------------------------------|
            Bitmap bmpTemp = new Bitmap(342, 134);
            bmpTemp.SetResolution(240, 240);
            Graphics g1 = Graphics.FromImage(bmpTemp);
            g1.Clear(System.Drawing.Color.FromArgb(((int)(((byte)(6)))), ((int)(((byte)(55)))), ((int)(((byte)(122))))));
            g1.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g1.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
            g1.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
            SolidBrush drawBrushx = new SolidBrush(Color.White);
            Font F14 = new System.Drawing.Font("Microsoft Sans Serif", 14F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            g1.DrawString("EMPLOYEE", F14, drawBrushx, 73, 90);
            bmpTemp.RotateFlip(RotateFlipType.Rotate270FlipNone);
            g.DrawImage(bmpTemp, 0, 190);
            //|-------------------------------------------------------------------------------------------------------------|

        }

      
        private void btnPrint_Click(object sender, EventArgs e)
        {
            try
            {
                if (printDialog1.ShowDialog(this) == DialogResult.OK)
                {
                    PageSettings Psettings = printDocument1.DefaultPageSettings;
                    printDocument1.PrinterSettings = printDialog1.PrinterSettings;
                    foreach (PaperSize ps in printDocument1.PrinterSettings.PaperSizes)
                    {
                        if (ps.PaperName == "A8")
                        {
                            Psettings.PaperSize = ps;
                            break;
                        }
                    }
                    printDocument1.Print();
                }
                Application.DoEvents();
            }
            catch
            {
                MessageBox.Show("Không tìm thấy máy in");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void LoadLanguage()
        {
            this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref this.toolStrip1);
        }

       
    }   
}
